import * as step from '@flow-step/step-toolkit'
import * as authUtil from './auth-helper'
import {IGitSourceParams} from './git-source-params'
import {GitOutput} from './git-command-helper'

export async function updateGitConfigSettings(
    action: string,
    input: IGitSourceParams,
    useInnerApiHost: boolean,
    updateConfigFn: (action: string,workPath: string, url1: string, url2: string ) => Promise<GitOutput>
) {
    const gitHost = authUtil.getHost(input.gitRepo)
    step.info(`git host name is: ${gitHost}`)
    if (gitHost === undefined) {
        return
    }
    const flowFeatureCodeupInternalDomain = process.env.FLOW_FEATURE_CODEUP_INTERNAL_DOMAIN;
    const flowFeatureCodeupInternalIP = process.env.FLOW_FEATURE_CODEUP_INTERNAL_IP;
    let gitInnerHost = gitHost;

   // 当 clone codeup 代码库且 FLOW_FEATURE_CODEUP_INTERNAL_DOMAIN / FLOW_FEATURE_CODEUP_INTERNAL_IP 不为空时，
   // 使用 $FLOW_FEATURE_CODEUP_INTERNAL_DOMAIN 域名进行 clone，且 clone 结束后恢复原 git_host 配置
    if(useInnerApiHost){
        if (gitHost === "codeup.aliyun.com" && flowFeatureCodeupInternalDomain && flowFeatureCodeupInternalIP) {
            gitInnerHost = flowFeatureCodeupInternalDomain;
            step.info(`gitInnerHost = ${gitInnerHost}`);
        }
    }
    if (input.httpUrlWithCredential && input.user !== undefined && input.password != undefined) {
        step.info(`add git config settings for http url with credential`)
        let user = encodeURIComponent(input.user)
        let password = encodeURIComponent(input.password)

        if(gitHost != gitInnerHost){
            //   替换 http 带账密协议为内网域名
            await updateConfigFn(
                action,
                input.workDir,
                `url.${input.httpPolicy}://${user}:${password}@${gitInnerHost}.insteadOf`,
                `http://${user}:${password}@${gitHost}`

            )
            await updateConfigFn(
                action,
                input.workDir,
                `url.${input.httpPolicy}://${user}:${password}@${gitInnerHost}.insteadOf`,
                `https://${user}:${password}@${gitHost}`

            )

        }
        await updateConfigFn(
            action,
            input.workDir,
            `url.${input.httpPolicy}://${user}:${password}@${gitInnerHost}/.insteadOf`,
            `git@${gitHost}/`

        )
        await updateConfigFn(
            action,
            input.workDir,
            `url.${input.httpPolicy}://${user}:${password}@${gitInnerHost}/.insteadOf`,
            `git@${gitHost}:`

        )
        await updateConfigFn(
            action,
            input.workDir,
            `url.${input.httpPolicy}://${user}:${password}@${gitInnerHost}.insteadOf`,
            `http://${gitHost}`

        )
        await updateConfigFn(
            action,
            input.workDir,
            `url.${input.httpPolicy}://${user}:${password}@${gitInnerHost}.insteadOf`,
            `https://${gitHost}`

        )
    } else if (input.sshUrl === true) {
        step.info(`add git config settings for ssh url`)

        if(gitHost != gitInnerHost){

            // 替换 ssh 协议为内网域名

            await updateConfigFn(
                action,
                input.workDir,
                `url.git@${gitInnerHost}:.insteadOf`,
                `git@${gitHost}/`

            )
            await updateConfigFn(
                action,
                input.workDir,
                `url.git@${gitInnerHost}:.insteadOf`,
                `git@${gitHost}:`

            )

        }
        await updateConfigFn(
            action,
            input.workDir,
            `url.git@${gitInnerHost}:.insteadOf`,
            `http://${gitHost}/`

        )
        await updateConfigFn(
            action,
            input.workDir,
            `url.git@${gitInnerHost}:.insteadOf`,
            `https://${gitHost}/`

        )
    }
}
